import json
import os
import traceback

import mysql_connect
from ZhihuComment import ZhihuComment


# def extract_one_data(file_name: str):
#     json_text: str
#     with open(f"other_files/comment_jsons/essay_jsons/{file_name}", 'r', encoding='utf-8') as r_file:
#         json_text = r_file.read()
#     # print(json_text)
#     data_dict: dict = json.loads(json_text)
#     if 'error' in data_dict.keys():
#         print("该评论为空，跳过")
#         return
#         # print(data_dict["data"] )
#     if len(data_dict["data"]) == 0:
#         print("该评论为空，跳过")
#         return
#     # print(data_dict["data"][0])
#     # # print(data_dict["data"])
#     commit_list: list[dict] = []
#     for comment in data_dict['data'] :
#         children = comment["child_comments"]
#         del comment["child_comments"]
#         comment["parent_id"] = None
#         parent_id = comment['id']
#         print(comment["parent_id"])
#         commit_list.append(comment)
#         # print(children)
#         # print(comment)
#         # break
#         while len(children) != 0:
#             for child in children:
#                 children = c
#
#     # if '404' in json_text:
#     #     print(json_text)
#     #     # quit()


def extract_one_data(file_name: str) -> list[ZhihuComment]:
    result_list = []
    json_text: str
    with open(f"other_files/comment_jsons/answer_jsons/{file_name}", 'r', encoding='utf-8') as r_file:
        json_text = r_file.read()
    # print(json_text)

    data_dict: dict = json.loads(json_text)
    if 'error' in data_dict.keys():
        print("该评论为空，跳过")
        return result_list
        # print(data_dict["data"] )
    if len(data_dict["data"]) == 0:
        print("该评论为空，跳过")
        return result_list
    data_list = convert_json_to_list(data_dict["data"])

    principal_id = int(file_name.split(".")[0])

    for data_one in data_list:
        zhihu_comment = ZhihuComment()
        # if "-" in data_one["id"]:
        #     data_one["id"] = data_one["id"].replace("-","")
        zhihu_comment.id = data_one["id"]
        zhihu_comment.type = 2
        zhihu_comment.content = data_one["content"]
        zhihu_comment.principal_id = principal_id
        zhihu_comment.parent_id = data_one["parent_id"]
        zhihu_comment.comment_level = data_one["comment_level"]

        zhihu_comment.username = data_one["author"]["member"]["name"]
        zhihu_comment.image_url = data_one["author"]["member"]["avatar_url"]
        zhihu_comment.introduction = data_one["author"]["member"]["headline"]
        result_list.append(zhihu_comment)
    return result_list


def convert_json_to_list(comment, parent_id=None, comment_level=1):
    result = []
    for child_comment in comment:

        comment_dict = {
            "id": child_comment["id"],
            "content": child_comment["content"],
            "parent_id": parent_id,
            "comment_level": comment_level
        }
        comment_dict.update(child_comment)  # 添加其他键值对
        comment_dict.pop("child_comments", None)  # 去除 child_comments 键
        result.append(comment_dict)

        if "child_comments" in child_comment and isinstance(child_comment["child_comments"], list):
            child_comments = child_comment["child_comments"]
            if child_comments:
                result.extend(convert_json_to_list(child_comments, child_comment["id"], comment_level + 1))

    return result


def save_in_mysql(data_list: list[ZhihuComment],conn,cursor):
    # conn, cursor = mysql_connect.get_conn()
    try:
        sql = """
            insert ignore into zhihu_comment 
                (id,type,principal_id,comment_level,parent_id,content,username,image_url,introduction,url)
                values 
                (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)
        """
        insert_list = [(comment.id, comment.type, comment.principal_id, comment.comment_level, comment.parent_id,
                        comment.content, comment.username, comment.image_url, comment.introduction, comment.url) for
                       comment in data_list]
        cursor.executemany(sql,insert_list)
        conn.commit()
    except:
        print("大失败")
        print(data_list)
        traceback.print_exc()
    finally:
        pass


def get_already_data():
    conn, cursor = mysql_connect.get_conn()
    result_list: list[int] = []
    try:
        sql = """
                SELECT
	zc.principal_id
FROM
	zhihu_comment AS zc
WHERE
	zc.type = 2
            """
        cursor.execute(sql)
        result = cursor.fetchall()
        result_list = [ id[0] for id in result ]

    except:
        print("大失败")
        traceback.print_exc()
    finally:
        cursor.close()
        conn.close()
    return result_list

def main():
    conn,cursor = mysql_connect.get_conn()
    try:
        file_name_list = os.listdir("other_files/comment_jsons/answer_jsons")
        already_data_list = get_already_data()
        for index, file_name in enumerate(file_name_list):

            print(f"现在是第{index}个文件，文件名{file_name},总共有{len(file_name_list)}个")
            if int( file_name.split('.')[0] ) in already_data_list:
                print("已存在，跳过")
                continue
            data_list = extract_one_data(file_name)

            save_in_mysql(data_list,conn,cursor)
            # break
    except:
        print("整体大失败")
        traceback.print_exc()
    finally:
        cursor.close()
        conn.close()


if __name__ == "__main__":
    main()
